layui.use(['table', 'HttpRequest', 'func', 'form', 'laydate', 'gunsSelect'], function () {
    var $ = layui.$;
    var table = layui.table;
    var HttpRequest = layui.HttpRequest;
    var func = layui.func;
    var form = layui.form;
    var laydate = layui.laydate;
    var gunsSelect = layui.gunsSelect;

    /**
     * 初始化参数
     */
    var VoteHistory = {
        tableId: "voteHistoryTable"
    };

    /**
     * 初始化表格的列
     */
    VoteHistory.initColumn = function () {
        return [[
			{field: 'voteId', hide: true, title: '主键'},
			{field: 'workId', align: "center", title: '作品编号'},
			{field: 'workName', align: "center", title: '作品（作者+标题）'},
			{field: 'voteIp', align: "center", title: '投票IP'},
			{field: 'voteClient', align: "center", title: '投票客户端'},
			{field: 'createdBy', align: "center", title: '创建人'},
			{field: 'createdTime', align: "center", title: '投票时间'},
           //{align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    VoteHistory.search = function () {
        var queryData = {};
        queryData['workId'] = $("#workId").val();
        queryData['voteIp'] = $("#voteIp").val();
        queryData['voteClient'] = $("#voteClient").val();
        queryData['createdBy'] = $("#createdBy").val();
        queryData['createdTime'] = $("#createdTime").val();
        queryData['updatedBy'] = $("#updatedBy").val();
        queryData['updatedTime'] = $("#updatedTime").val();
        table.reload(VoteHistory.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 添加对话框
     */
    VoteHistory.openAddDlg = function () {
        func.open({
            title: '添加',
            content: Feng.ctxPath + '/view/voteHistory/add',
            tableId: VoteHistory.tableId
        });
    };

    /**
     * 编辑对话框
     *
     * @param data 点击按钮时候的行数据
     */
    VoteHistory.openEditDlg = function (data) {
        func.open({
            title: '修改',
			content: Feng.ctxPath + '/view/voteHistory/edit?voteId=' + data.voteId,
            tableId: VoteHistory.tableId
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    VoteHistory.onDeleteItem = function (data) {
        var operation = function () {
            var request = new HttpRequest(Feng.ctxPath + "/voteHistory/delete", 'post', function (data) {
                Feng.success("删除成功!");
                table.reload(VoteHistory.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
			request.set("voteId", data.voteId);
            request.start(true);
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        title: '投票记录',
        elem: '#' + VoteHistory.tableId,
        url: Feng.ctxPath + '/voteHistory/page',
        toolbar: true,
        defaultToolbar: ['filter', 'print','exports'],
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: VoteHistory.initColumn(),
        request: {pageName: 'pageNo', limitName: 'pageSize'},
        parseData: Feng.parseData
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        VoteHistory.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        VoteHistory.openAddDlg();
    });

    // 工具条点击事件
    table.on('tool(' + VoteHistory.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            VoteHistory.openEditDlg(data);
        } else if (layEvent === 'delete') {
            VoteHistory.onDeleteItem(data);
        }
    });

});